﻿<?php
	ini_set("display_errors", true);
	error_reporting(E_ALL);
	require("../includes/constants.php");
	require("../includes/functions.php");
	//Set the timezone
	$satime=query("set time_zone='+03:00'");
	$satime=query("select CURTIME()");
	$satime=$satime[0]['CURTIME()'];
	$satime=explode(":",$satime);
	//Save hours only.
	$satime=$satime[0];
	$today=query("SELECT CURDATE()");
	//$today=strtotime($today[0]['CURDATE()']);
	//Get list of expiring contracts with the client still in.
	//contract 2 means mo2agar
	$info=query("SELECT * FROM `contracts` WHERE `contract_status` = '2' AND `miladi_out` <= ?",$today);
	foreach ($info as $cont)
	{
	$ainfox=query("SELECT * FROM `contract_apartments` INNER JOIN `apartments` ON contract_apartments.apartment_id=apartments.id WHERE contract_apartments.contract_id=?",$cont['contract_id']);
	foreach ($ainfox as $ainfo)
	{
		
		//Check for contracts that expired and the time is past 2pm or 4pm
		/*if(($today>$cont['miladi_out']&&$ainfo['status']==5)||($satime>=16&&$ainfo['status']==5))
		//check for contracts that expired and the time is past 4pm and extend it.
		{
		$ndate=date("Y-m-d",(strtotime($cont['miladi_out'])+86400));
		$ntotal=(int) round($cont['total']/$cont['period'])+$cont['total'];
		$nperiod=$cont['period']+1;
		query("UPDATE `contracts` SET `period` = ?,`total` = ?,`miladi_out`=? WHERE `contract_id` = ?",$nperiod,$ntotal,$ndate,$cont['contract_id']);
		query("UPDATE `apartments` SET `status` = '1' WHERE `id` = ?",$cont['apartment_id']);
		} */
		if (($today>$cont['miladi_out']&&$ainfo['status']==1)||(($satime >=14)&&$ainfo['status']==1))
		{
			//update the apartment status
			query("UPDATE `apartments` SET `status` = '5' WHERE `id` = ?",$ainfo['apartment_id']);
		}
		elseif(( ($satime>=8&&$satime<9) && $ainfo['status']==1))
		{
			//update the apartment status
			query("UPDATE `apartments` SET `status` = '5' WHERE `id` = ?",$ainfo['apartment_id']);
			$cinfo=query("SELECT contract_id,client_id,clients.name,clients.mobile FROM `contracts` INNER JOIN `clients` ON contracts.client_id=clients.id WHERE contracts.contract_id=?",$cont['contract_id']);
			$mobile = $cinfo['mobile'];
		if (substr($mobile, 0, 1) === '0') {
		   $mobile = substr($mobile, 1);    
		   $mobile = "+966".$mobile;
		}
		else
		  $mobile = "+966".$mobile;
		$cinfo=$cinfo[0];
			$msg="عزيزي ".$cinfo['name']."
	نتمنى ان تكونوا قضيتم معنا اوقات سعيدة ونتمنى ان تكون خدماتنا قد نالت رضائكم ونتطلع دائما لخدمتكم.
	ان اقامتكم معنا بموجب العقد مع سعادتكم سوف تنتهي اليوم في حال رغبتكم التمديد نأمل الاتصال بالاستقبال على التحويلة (صفر) او 100";
			sendSMS($mobile,'Lemar',utf8ToUnicode($msg));
		}
	}
}

//Get contracts starting today
$info2=query("SELECT * FROM `contracts` WHERE `contract_status` = '1' AND `mildai_from` <= ?",$today);
foreach ($info2 as $contr)
{
	$ainfozx=query("SELECT * FROM `contract_apartments` INNER JOIN `apartments` ON contract_apartments.apartment_id=apartments.id WHERE contract_apartments.contract_id=?",$contr['contract_id']);
	
	foreach ($ainfozx as $ainfoz)
	{
		//Check status
		if($ainfoz['status']==6&&($contr['mildai_from']<$today||($contr['mildai_from']==$today&&$satime>=16))) 
		//Change contract status
		{
		//Contract Status 5 is a new status to indicate that someone didn't come in time
    query("UPDATE `apartments` SET `status` = '9' WHERE `id` = ?",$ainfoz['apartment_id']);

		//query("UPDATE `apartments` SET `status` = '2' WHERE `id` =?",$contr['apartment_id']);
		}
	}

}
?>
